package com.yuyou.fn.report.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.service.IStudentRegService;
import com.yuyou.fn.educational.service.ITeacherService;
import com.yuyou.fn.educational.vo.StudentRegQueryVo;
import com.yuyou.fn.educational.vo.StudentRegQueryVo2;
import com.yuyou.fn.platform.permission.DataPermission;
import org.springframework.stereotype.Service;

import com.yuyou.fn.report.mapper.StudentRegDetailMapper;
import com.yuyou.fn.report.entity.StudentRegDetail;
import com.yuyou.fn.report.service.IStudentRegDetailService;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 *
 * StudentRegDetail 表数据服务层接口实现类
 *
 */
@Service
public class StudentRegDetailServiceImpl extends SuperServiceImpl<StudentRegDetailMapper, StudentRegDetail,Long> implements IStudentRegDetailService {
    @Resource
    private ITeacherService teacherService;

    @Resource
    private IStudentRegService studentRegService;

    @Override
    @Transactional
    public void updateSeniorByRegId(Long regId, Long seniorId) {
        Teacher teacher=teacherService.findById(seniorId);
        if (teacher==null){
            throw new BusException("查不到该教师");
        }
        StudentRegDetail studentReg=new StudentRegDetail();
        studentReg.setRegId(regId);
        studentReg.setSeniorId(seniorId);
        studentReg.setSeniorName(teacher.getTeacherName());
        this.updateSelectiveById(studentReg);
        studentRegService.updateSeniorByRegId(regId,seniorId);
    }

    @Override
    public Page<StudentRegDetail> findStudentRegByPermissionQuery(DataPermission dataPermission, StudentRegQueryVo studentRegQueryVo, int pageNo, int pageSize) {
        if (Utils.checkListEmpty(dataPermission.getBusTeamIds())){
            return new Page<>(0,0);
        }
        Example<StudentRegDetail> example = StudentRegDetail.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria , studentRegQueryVo);
        criteria.andIn("busTeamId",dataPermission.getBusTeamIds());
        return findPageByExample(example,pageNo,pageSize );
    }

    @Override
    public Page<StudentRegDetail> findStudentRegByPermissionQuery2(DataPermission dataPermission, StudentRegQueryVo2 studentRegQueryVo, int pageNo, int pageSize) {
        if (Utils.checkListEmpty(dataPermission.getBusTeamIds())){
            return new Page<>(0,0);
        }
        Example<StudentRegDetail> example = StudentRegDetail.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria , studentRegQueryVo);
        criteria.andIn("busTeamId",dataPermission.getBusTeamIds());
        return findPageByExample(example,pageNo,pageSize );
    }


}